import os


# 批量更改文件名中相同部分的内容
def ModifyFileName(filepath, originalname, modifyname):
    """ 修改文件名内容
    :param filepath: 文件路径
    :param originalname: 待修改内容
    :param modifyname: 修改后内容
    :return: void
    """
    filename = os.path.basename(filepath)
    if originalname in filename:
        replacename = filename.replace(originalname, modifyname)
        replacepath = os.path.dirname(filepath) + '\\' + replacename
        try:
            os.rename(filepath, replacepath)
            print(filename + " 已修改为：" + replacename)
        except:
            print(filename + "文件路径不存在")


def ModifyAll(dirpath, originalname, modifyname):
    ''' 修改文件夹内所有文件
    :param dirpath: 文件夹路径
    :param originalname: 待修改内容
    :param modifyname: 修改后内容
    :return: void
    '''
    for root, dirs, files in os.walk(dirpath):
        for file in files:
            ModifyFileName(os.path.join(root, file), originalname, modifyname)
    print("已全部修改！")


if __name__ == '__main__':
    # print("请输入文件夹路径：")
    dirpath = "F:\B站视频\练完即可就业，从入门到进阶，基础到框架，web_k8s_docker"
    # dirpath = input()
    # print("请输入需要修改的内容：")
    originalname = " （"
    # originalname = input()
    # print("请输入修改后的内容：")
    modifyname = ""
    # modifyname = input()
    print("正在执行中~~~~~~~~~")
    ModifyAll(dirpath, originalname, modifyname)
